package bg.btanev.eightyfour.client.view;

import java.util.LinkedList;
import java.util.List;

import bg.btanev.eightyfour.client.presenter.listener.OpenPanelListener;
import bg.btanev.eightyfour.client.presenter.listener.TargetActionMenuOpenListener;
import bg.btanev.eightyfour.shared.dto.TargetDto;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 *
 * @author B.Tanev
 *
 */
public class TargetListView extends Composite {

	private final VerticalPanel targetsListVerticalPanel;
	private final List<OpenPanelListener> openPanelListeners = new LinkedList<OpenPanelListener>();
	private List<TargetDto> targets;
	private String[] targetMenus;
	private TargetActionMenuOpenListener targetMenuListener;

	public TargetListView() {
		targetsListVerticalPanel = new VerticalPanel();
		initWidget(targetsListVerticalPanel);
		targetsListVerticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		targetsListVerticalPanel.setWidth("100%");
		targetsListVerticalPanel.getElement().getStyle().setPadding(5.0, Unit.PX);
		targetsListVerticalPanel.setSpacing(1);
	}

	public List<TargetDto> getTargets() {
		return targets;
	}

	public void setTargets(List<TargetDto> targets) {
		this.targets = targets;
		UpdateTargets();
	}

	public void setTargetMenuListener(TargetActionMenuOpenListener targetMenuListener) {
		this.targetMenuListener = targetMenuListener;
	}

	public void setTargetMenus(String[] targetMenus) {
		this.targetMenus = targetMenus;
	}

	private void UpdateTargets() {
		targetsListVerticalPanel.clear();

		if (targets == null) {
			return;
		}

		for (int i = 0; i < targets.size(); i++) {
			TargetListElementView targetListElement = new TargetListElementView();
			if (this.targetMenuListener != null) {
				targetListElement.addTargetActionMenuOpenListener(this.targetMenuListener);
			}
			if (this.targetMenus != null) {
				targetListElement.addActionMenus(this.targetMenus);
			}
			targetListElement.setTarget(targets.get(i));
			targetListElement.getOpenHandlers().addOpenHandler(new OpenHandler<DisclosurePanel>() {
				@Override
				public void onOpen(OpenEvent<DisclosurePanel> event) {
					Widget parent = event.getTarget().getParent();
					if (parent instanceof TargetListElementView) {
						TargetListElementView targetContainer = (TargetListElementView) parent;

						HasWidgets dataPanel = targetContainer.getDataPanel();
						TargetDto target = targetContainer.getTarget();

						notifyListeners(dataPanel, target);
					}
				}
			});

			targetsListVerticalPanel.add(targetListElement);
		}
		targetsListVerticalPanel.setBorderWidth(1);
		targetsListVerticalPanel.setVisible(true);
	}

	private void notifyListeners(HasWidgets dataPanel, TargetDto target) {
		for (OpenPanelListener listener : openPanelListeners) {
			listener.onOpen(dataPanel, target);
		}
	}

	public void addOpenPanelListener(OpenPanelListener listener) {
		this.openPanelListeners.add(listener);
	}

	public HasWidgets getMainContainer() {
		return targetsListVerticalPanel;
	}
}
